﻿Imports System.Data

Partial Class Interfaz_frmBodegaStock
    Inherits System.Web.UI.Page
    Dim CL As New Logica.ModuloBodega.ControladorLogica
    Dim dtDatos As New DataTable
    Dim dataset As New DataSet
    Dim data As New DataTable

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CL = Logica.ModuloBodega.ControladorLogica.getInstance

        If CL.visibilidadesFormsInternos("Bodega", Session("user").ToString) = False Then
            Response.Redirect("Index.aspx")
        End If
        Dim arrayItem As New ArrayList
        Dim i As Integer

        arrayItem = CL.ObtenerMaterial()

        If Session("stockBodega") Is Nothing Then
            Session("stockBodega") = dtDatos
        Else
            dtDatos = DirectCast(Session("stockBodega"), DataTable)
        End If

        If Not Page.IsPostBack Then
            DropMaterial.Items.Add("Mostrar Todos")
            For i = 0 To arrayItem.Count - 1
                DropMaterial.Items.Add(DirectCast(arrayItem.Item(i), Material).Nombre)
            Next

            dtDatos.Columns.Add(New DataColumn("NOMBRE"))
            dtDatos.Columns.Add(New DataColumn("DESCRIPCION"))
            dtDatos.Columns.Add(New DataColumn("CANTIDAD"))
            dtDatos.Columns.Add(New DataColumn("CODMATERIAL"))
            dtDatos.Columns.Add(New DataColumn("FECHAINGRESO"))
            dtDatos.Columns.Add(New DataColumn("UBICACIONCOLUMNA"))
            dtDatos.Columns.Add(New DataColumn("UBICACIONFILA"))
            Me.GridMateriales.DataBind()
            Me.GridMateriales.DataSource = Me.dtDatos

        End If

        Me.GridMateriales.DataBind()
        Me.GridMateriales.DataSource = Me.dtDatos


    End Sub

#Region "Funcionalidades"
    Public Sub Mensaje(ByVal msg As String)
        Dim alertScript As String = "<script language=JavaScript runat=server>"
        alertScript &= "alert('" & msg & "');"
        alertScript &= "</script>"
        ClientScript.RegisterClientScriptBlock(Page.GetType(), "clientScrip", alertScript)
    End Sub

    Private Sub vaciaGrid()
        data = DirectCast(Session("stockBodega"), DataTable)
        If Not Data Is Nothing Then
            Data.Clear()
            Session("stockBodega") = data
        End If
    End Sub

    Private Sub CargarMaterial()
        Dim query As String
        Dim array As New ArrayList
        Dim i As Integer
        Dim optionMat As String = DropMaterial.Text

        If optionMat = "Mostrar Todos" Then
            array = CL.ObtenerMaterial()
        Else
            query = "select * from material where nombre ='" + DropMaterial.Text + "'"
            array = CL.ObtenerMaterial(query)
        End If


        Try
            If array.Count = 0 Then
                Me.Mensaje("No existe ningun material asociado al criterio de busqueda seleccionado")
            Else
                For i = 0 To array.Count - 1
                    Dim drDatos As DataRow = dtDatos.NewRow
                    drDatos.Item("NOMBRE") = DirectCast(array.Item(i), Material).Nombre
                    drDatos.Item("DESCRIPCION") = DirectCast(array.Item(i), Material).DescripcionFormal
                    drDatos.Item("CANTIDAD") = DirectCast(array.Item(i), Material).RefMaterialDatos.Cantidad
                    drDatos.Item("CODMATERIAL") = DirectCast(array.Item(i), Material).Codigo
                    drDatos.Item("FECHAINGRESO") = DirectCast(array.Item(i), Material).RefMaterialDatos.FechaIngreso
                    drDatos.Item("UBICACIONCOLUMNA") = DirectCast(array.Item(i), Material).RefMaterialDatos.UbicacionColumna
                    drDatos.Item("UBICACIONFILA") = DirectCast(array.Item(i), Material).RefMaterialDatos.UbicacionFila
                    dtDatos.Rows.Add(drDatos)
                Next
                Me.GridMateriales.DataSource = dtDatos
                Me.GridMateriales.DataBind()
                Me.GridMateriales.ForeColor = Drawing.Color.Blue
            End If
        Catch ex As Exception
            Me.Mensaje(ex.ToString)
        End Try

    End Sub
#End Region

#Region "Botones"

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        vaciaGrid()
        CargarMaterial()
    End Sub
#End Region

    
End Class
